package org.fuys.coder.domain.act.support.service.impl;

import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.act.activity.model.vo.CommonActVO;
import org.fuys.coder.domain.act.activity.service.ICommonActService;
import org.fuys.coder.domain.act.lottery.model.vo.activity.LotteryActVO;
import org.fuys.coder.domain.act.lottery.service.activity.ILotteryActivityService;
import org.fuys.coder.domain.act.support.model.req.ActPageGetReq;
import org.fuys.coder.domain.act.support.model.vo.ActTypeVO;
import org.fuys.coder.domain.act.support.model.vo.ActVO;
import org.fuys.coder.domain.act.support.service.IActSupportService;
import org.fuys.coder.common.entity.PageDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.act.support.service.impl
 * @className: ActSupportServiceImpl
 * @author: WangJing
 * @description: 活动辅助服务实现类
 * @date: 2024/8/25 22:33
 * @version: 1.0
 */
@Service
public class ActSupportServiceImpl implements IActSupportService {

    @Resource
    private ILotteryActivityService lotteryActivityService;

    @Resource
    private ICommonActService commonActService;


    @Override
    public ActVO getActById(Long actId, Integer actType) {
        ActVO actVO=null;
        if(actType.equals(ActTypeVO.COMMON)){
           CommonActVO vo = commonActService.getActById(actId,1);
           if(vo!=null){
               actVO=new ActVO();
               actVO.setActData(vo);
           }
        }else if(actType.equals(ActTypeVO.LOTTERY)){
           LotteryActVO vo = lotteryActivityService.getLotteryById(actId,1);
           if(vo!=null){
               actVO=new ActVO();
               actVO.setActData(vo);
           }
        }else{
            throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT);
        }
        return actVO;
    }

    @Override
    public List<ActVO> getCurHotActivity() {
        List<ActVO> result=new ArrayList<>();
        final List<ActVO> curHotAct =
                commonActService.getCurHotAct(2);
        final List<ActVO> curHotLottery =
                lotteryActivityService.getCurHotAct(2);
        return computeResultAct(curHotAct, curHotLottery, result);
    }

    @Override
    public Pair<List<ActVO>,Long> getPageAct(ActPageGetReq req) {
        final PageDTO pageDTO = req.getPageDTO();
        final Integer curPage = pageDTO.getCurPage();
        final Integer avgNum = pageDTO.getPageNum()/2;
        pageDTO.setPageNum(avgNum);
        final Pair<List<ActVO>,Long> commonPage = commonActService.getPage(pageDTO);
        long count=0;
        List<ActVO> commonList=null;
        if(commonPage!=null){
            commonList=commonPage.getLeft();
            count+=commonPage.getRight();
        }
        List<ActVO> lotteryList=null;
        final Pair<List<ActVO>,Long> lotteryPage = lotteryActivityService.getPage(pageDTO);
        if(lotteryPage!=null){
            lotteryList=lotteryPage.getLeft();
            count+=lotteryPage.getRight();
        }
        List<ActVO> result=new ArrayList<>();
        final List<ActVO> voList = computeResultAct(commonList, lotteryList, result);
        return new Pair<>(voList,count);
    }

    private List<ActVO> computeResultAct(List<ActVO> commonPage, List<ActVO> lotteryPage, List<ActVO> result) {
        if(!ObjectUtils.isEmpty(commonPage)){
            result.addAll(commonPage);
        }
        if(!ObjectUtils.isEmpty(lotteryPage)){
            result.addAll(lotteryPage);
        }
        return result;
    }
}
